/**
 * All rights Reserved, Designed By www.beonelot.com
 * @Package: com.jinxin.platform.eleinspect.controller
 * @Filename: EleinspectPointManageController.java
 * @Description: TODO(EleinspectPointManageController)
 * @author: 金鑫智慧
 * @date: 2020-03-30
 * @version: V1.0
 * @Copyright: 2020 www.beonelot.com Inc. All rights reserved.
 * 注意：本内容仅限于重庆金鑫智慧科技有限公司内部传阅，禁止外泄以及用于其他的商业目的
 */
package com.jinxin.platform.cdelecinspect.controller;


import com.alibaba.fastjson.JSONArray;
import com.jinxin.platform.base.common.pojo.JsonResult;
import com.jinxin.platform.cdelecinspect.mapper.pojo.*;
import com.jinxin.platform.cdelecinspect.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * TODO(EleinspectPointManageController)
 * @Classn: EleinspectPointManageController
 * @author Lynn
 * @date 2020-03-30
 */
@RestController
@RequestMapping("/eleinspect/point")
public class EleinspectPointManageController {

    @Autowired
    EleinspectPointManageService eleinspectPointManageService;

    @Autowired
    EleinspectMappingPointLineService eleinspectMappingPointLineService;

    @Autowired
    EleinspectLineManageService eleinspectLineManageService;

    @Autowired
    EleinspectItemsService eleinspectItemsService;

    @Autowired
    EleinspectMappingPointItemsService eleinspectMappingPointItemsService;

    @RequestMapping("/getPointTable")
    public Map<String,Object> getPointTable(Integer page, Integer limit,String pointName,String beginDate,String endDate){
        HashMap<String,Object> param = new HashMap<>();
        if (!StringUtils.isEmpty(pointName)){
            param.put("pointName", pointName);
        }
        if (!StringUtils.isEmpty(beginDate)){
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            try{
                Date date = simpleDateFormat.parse(beginDate);
                param.put("beginDate", date);
            }catch (ParseException e){
                e.printStackTrace();
            }
        }
        if (!StringUtils.isEmpty(endDate)){
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            try{
                Date date = simpleDateFormat.parse(endDate);
                param.put("endDate", date);
            }catch (ParseException e){
                e.printStackTrace();
            }
        }
        param.put("page", page );
        param.put("limit", limit);
        List<EleinspectPointManage> allPoint = eleinspectPointManageService.listPage(param);
        int pageCount = eleinspectPointManageService.listPageCount(param);
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("code",0);
        resultMap.put("msg","请求成功");
        resultMap.put("data",allPoint);
        resultMap.put("count",pageCount);
        return resultMap;
    }

    @RequestMapping("/savePointInfo")
    public Map<String,Object> savePointInfo(EleinspectPointManage eleinspectPointManage,int editIndex,String dataArrayJson,String deleteItemIdJson){
        Map<String,Object> resultMap = new HashMap<>();
//        if (StringUtils.isEmpty(eleinspectPointManage.getPointInspectMethod())){
//            resultMap.put("code",204);
//            resultMap.put("msg","请选择打卡方式");
//            return resultMap;
//        }
        if (editIndex == 0){
            eleinspectPointManage.setPointCreateTime(new Date());
            eleinspectPointManage.setPointId(UUID.randomUUID().toString());
            //二维码信息
//            String msg = eleinspectPointManage.getPointId();
//            String path = QRCodeUtil.getBarCode(msg);
//            eleinspectPointManage.setPointSystemInfo(path);
        }
        if (!StringUtils.isEmpty(dataArrayJson)){
            List<EleinspectItems> eleinspectItems = JSONArray.parseArray(dataArrayJson, EleinspectItems.class);
            if (eleinspectItems.size() > 0){
                int i = 0;
                if (!StringUtils.isEmpty(eleinspectPointManage.getPointId())){
                    eleinspectMappingPointItemsService.deleteByPointId(eleinspectPointManage.getPointId());
                }
                for (EleinspectItems eleinspectItem : eleinspectItems){
                    if (eleinspectItem == null){
                        i++;
                        continue;
                    }else{
                        //保存点位-项目信息
                        EleinspectMappingPointItems eleinspectMappingPointItems = new EleinspectMappingPointItems();
                        eleinspectMappingPointItems.setMappingItemsId(eleinspectItem.getItemsId());
                        eleinspectMappingPointItems.setMappingPointId(eleinspectPointManage.getPointId());
                        eleinspectMappingPointItems.setMappingCreateTime(new Date());
                        eleinspectMappingPointItemsService.insert(eleinspectMappingPointItems);
                    }
                }

                if (i == eleinspectItems.size()){
                    resultMap.put("code",204);
                    resultMap.put("msg","请添加项目");
                    return resultMap;
                }
                //删除点位-项目
                if (!StringUtils.isEmpty(deleteItemIdJson)){
                    List<String> deleteItemIdList = JSONArray.parseArray(deleteItemIdJson, String.class);
                    if (deleteItemIdList.size() > 0){
                        for (String itemId : deleteItemIdList){
                            if (itemId == null){
                                continue;
                            }else{
                                eleinspectMappingPointItemsService.deleteByItemId(itemId,eleinspectPointManage.getPointId());
                            }
                        }
                    }
                }
                if (editIndex == 1){
                    //更新
                    eleinspectPointManageService.updateByKey(eleinspectPointManage);
                }else{
                    //保存点位信息
                    eleinspectPointManageService.savePointInfo(eleinspectPointManage);
                }
            }else{
                resultMap.put("code",204);
                resultMap.put("msg","请添加项目");
                return resultMap;
            }
        }

        resultMap.put("code",0);
        resultMap.put("msg","操作成功");
        return resultMap;
    }


    @RequestMapping("/deletePoint")
    public Map<String,Object> deletePoint(String pointId){
        Map<String,Object> resultMap = new HashMap<>();
        EleinspectPointManage eleinspectPointManage = eleinspectPointManageService.selectByKey(pointId);
        //判断被删除点位是否被绑定
        if (eleinspectPointManage.getPointUsedStatus() == 1){
            EleinspectMappingPointLine eleinspectMappingPointLine = eleinspectMappingPointLineService.selectByPointId(eleinspectPointManage.getPointId());
            EleinspectLineManage eleinspectLineManage = eleinspectLineManageService.selectByKey(eleinspectMappingPointLine.getMappingLineId());
            resultMap.put("code",204);
            resultMap.put("msg","该点位已绑定[ " + eleinspectLineManage.getLineName() + " ]，请先从路线中移除或删除该线路");
        }else{
            //删除点位-项目信息
            eleinspectMappingPointItemsService.deleteByPointId(pointId);
            //删除点位信息
            eleinspectPointManageService.deleteByPointId(pointId);
            resultMap.put("code",0);
            resultMap.put("msg","删除成功");
        }
        return resultMap;
    }

    @RequestMapping("/getItemTable")
    public Map<String,Object> getItemTable(Integer page, Integer limit){
        HashMap<String,Object> param = new HashMap<>();
        param.put("page", page );
        param.put("limit", limit);
        List<EleinspectItems> eleinspectItems = eleinspectItemsService.listPage(param);
        int pageCount = eleinspectItemsService.listPageCount(param);
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("code",0);
        resultMap.put("msg","请求成功");
        resultMap.put("data",eleinspectItems);
        resultMap.put("count",pageCount);
        return resultMap;
    }

    @RequestMapping("/getPointItemInfo")
    public Map<String,Object> getPointItemInfo(String mappingPointId){
        List lis1t = new ArrayList(2);
        HashMap<String,Object> resultMap = new HashMap<>();
        HashMap<String,Object> param = new HashMap<>();
        param.put("mappingPointId",mappingPointId);
        List<EleinspectItems> itemList = new ArrayList<>();
        List<EleinspectMappingPointItems> list = eleinspectMappingPointItemsService.list(param);
        for(EleinspectMappingPointItems eleinspectMappingPointItems: list){
            EleinspectItems eleinspectItems = eleinspectItemsService.selectByKey(eleinspectMappingPointItems.getMappingItemsId());
            itemList.add(eleinspectItems);
        }
        resultMap.put("code",0);
        resultMap.put("data",itemList);
        resultMap.put("msg","请求成功");
        return resultMap;
    }

    @RequestMapping("/getPointInfo")
    public JsonResult getPointInfo(String pointId) {
        if(StringUtils.isEmpty(pointId)){
            return JsonResult.error("点位不存在");
        }
        EleinspectPointManage pointInfo = eleinspectPointManageService.selectByKey(pointId);
        return JsonResult.ok(pointInfo);
    }


}
